package tt.dz.service.imp.base;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.ext.common.OmsStationOperDao;
import tt.dz.dao.ext.system.OmsOperListDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsEntityEnum;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.station.OmsStation;
import tt.dz.entity.system.OmsOperList;
import tt.dz.entity.system.OmsOperStation;
import tt.dz.service.base.OperatorStationService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class OperatorStationServiceImpl extends BaseService implements OperatorStationService{
	@Autowired
	private OmsStationOperDao operatorStationDao;
	@Autowired
	private OmsOperListDao omsOperListDao;
	@Override
	public JsonForList<OmsStation> findStationByUserId(String operId, Integer entity, OmsCommonParam param,
			OmsStation station) 
	{
		int total = 0;
		List<OmsStation> lst = null;
		try 
		{
			if ( entity!= null && (entity == OmsEntityEnum.SUPER_OPERMERCHANT.getValue() ||
					entity == OmsEntityEnum.ENTERPRISE.getValue()))
			{
				lst = operatorStationDao.pagingFind(param, station);
				if (param.isQueryTotal())
				{
					total = operatorStationDao.queryTotalByObject(station);
				}
			}
			else if (entity!= null && entity == OmsEntityEnum.OPERMERCHANT.getValue() )
			{
				lst = operatorStationDao.findPageOrAllByOperMerchant(param, station, operId);
				if (param.isQueryTotal())
				{
					total = operatorStationDao.findCountPageOrAllByOperMerchant(param, station, operId);
				}
			}
			else if (entity!= null && 
					(entity == OmsEntityEnum.OPRERMERCHANT_BRANCH.getValue() || 
							entity == OmsEntityEnum.MERCHANT.getValue()))
			{
				lst = operatorStationDao.findPageOrAllByEntityType(param, station, operId, entity);
				if (param.isQueryTotal())
				{
					total = operatorStationDao.findCountByEntityType(param, station, operId,entity);
				}
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public JsonCommon<String> addOperStations(List<OmsOperStation> operStations) 
	{
		try 
		{
			operatorStationDao.physicalDeleteOperStations(operStations);
			operatorStationDao.batchAddStationOperMerchant(operStations);
		} 
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			JsonCommon<String> jfl = new JsonCommon<>();
			jfl.setCode(MsgCodeEnum.JAVA_DB_EXEC_ERROE.getMsgCode());
			jfl.setMsg(MsgCodeEnum.JAVA_DB_EXEC_ERROE.getMsgInfo());
			return jfl;
		}
		return this.buildSuccessJsonCommon("success");
	}

	@Override
	public JsonCommon<String> deleteOperStations(List<OmsOperStation> operStations) {
		try 
		{
			operatorStationDao.batchDeleteStationOperMerchant(operStations);
		} 
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon("success");
	}

	@Override
	public JsonCommon<String> physicalDeleteOperStations(List<OmsOperStation> operStations) {
		try 
		{
			operatorStationDao.physicalDeleteOperStations(operStations);
		} 
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
		return this.buildSuccessJsonCommon("success");
	}

	@Override
	public JsonForList getUnallocatedStationsByOperId(String oper_id, String entity_type) {
		StringBuffer sql = new StringBuffer();
		List<Map<String, Object>> list = null;
		
		sql.append("select a.station_id,a.station_name,a.oper_merchant_id,a.district_code,a.district_name,a.circle,");
		sql.append("a.parking_type,a.parking_addr,a.total_charger,a.establish_time,a.station_type,a.is_allow_open,a.address,");
		sql.append("a.current_state,a.direct_num,a.alternate_num,a.tel_no,a.describes,a.entity_type,a.entity_id,a.valid_flag,a.review_state,a.review_text,");
		sql.append("a.create_time,a.create_oper_id,a.update_time,a.update_oper_id from oms_station a,oms_oper_list c where a.valid_flag=1 and c.oper_id= ?  and a.station_id not in ");
		sql.append("( select station_id  from oms_oper_station b where b.oper_id= ? ) ");
		
		if (entity_type.equals("1")){
			sql.append(" and a.oper_merchant_id = c.entity_id ");
		}else{
			sql.append(" and a.entity_id = c.entity_id ");
		}
		
		list = operatorStationDao.findDataBySql(sql.toString(), oper_id, oper_id);

		JsonForList json = new JsonForList();
		json.setList(list);
		json.setCode(JsonForList.SuccessCode);

		return json;
	}
	@Override
	public JsonForList getUnallocatedStationsForBranch(String merchant_oper_id,String branch_oper_id, String entity_type) {
		StringBuffer sql = new StringBuffer();
		List<Map<String, Object>> list = null;
		
		sql.append("select a.station_id,a.station_name,a.oper_merchant_id,a.district_code,a.district_name,a.circle,");
		sql.append("a.parking_type,a.parking_addr,a.total_charger,a.establish_time,a.station_type,a.is_allow_open,a.address,");
		sql.append("a.current_state,a.direct_num,a.alternate_num,a.tel_no,a.describes,a.entity_type,a.entity_id,a.valid_flag,a.review_state,a.review_text,");
		sql.append("a.create_time,a.create_oper_id,a.update_time,a.update_oper_id from oms_station a,oms_oper_list c where c.oper_id= ?  and a.station_id not in ");
		sql.append("( select station_id  from oms_oper_station b where b.oper_id= ? ) ");
		
		if (entity_type.equals("1")){
			sql.append(" and a.oper_merchant_id = c.entity_id ");
		}else{
			sql.append(" and a.entity_id = c.entity_id ");
		}
		
		list = operatorStationDao.findDataBySql(sql.toString(), merchant_oper_id, branch_oper_id);

		JsonForList json = new JsonForList();
		json.setList(list);
		json.setCode(JsonForList.SuccessCode);

		return json;
	}

	@Override
	public JsonForList getStationsByOperId(String oper_id) {
		OmsOperList omsOperList =new OmsOperList();
		omsOperList.setOper_id(oper_id);
		omsOperList = omsOperListDao.findOneExample(omsOperList, null, null);
		JsonForList json = new JsonForList();
		if(omsOperList!=null ){
			StringBuffer sql = new StringBuffer();
			List<Map<String, Object>> list = null;
			if(omsOperList.getAuth_range() == 1){  //操作权限：0：所归属实体下所有站；1：特定站；2：所有站（只针对企业用户）
				sql.append("select a.station_id,a.station_name,a.oper_merchant_id,a.district_code,a.district_name,a.circle,");
				sql.append("a.parking_type,a.parking_addr,a.total_charger,a.establish_time,a.station_type,a.is_allow_open,a.address,");
				sql.append("a.current_state,a.direct_num,a.alternate_num,a.tel_no,a.describes,a.entity_type,a.entity_id,a.valid_flag,a.review_state,a.review_text,");
				sql.append("a.create_time,a.create_oper_id,a.update_time,a.update_oper_id from oms_station a ");
				sql.append("where a.station_id in ( select station_id  from oms_oper_station b where b.oper_id= ? ) ");
				
				list = operatorStationDao.findDataBySql(sql.toString(),oper_id);

				json.setList(list);
				json.setCode(JsonForList.SuccessCode);
			}else{
				sql.append("selecta.station_id,a.station_name,a.oper_merchant_id,a.district_code,a.district_name,a.circle,");
				sql.append("a.parking_type,a.parking_addr,a.total_charger,a.establish_time,a.station_type,a.is_allow_open,a.address,");
				sql.append("a.current_state,a.direct_num,a.alternate_num,a.tel_no,a.describes,a.entity_type,a.entity_id,a.valid_flag,a.review_state,a.review_text,");
				sql.append("a.create_time,a.create_oper_id,a.update_time,a.update_oper_id from oms_station a where  1 = 1 ");
//				sql.append("( select station_id  from oms_oper_station b where b.oper_id= ? ) ");
				
				if (omsOperList.getEntity_type().equals(1)){ //1：运营商；2：运营商分支机构；3：厂商；4:商户；企业用户则为0
					sql.append("and a.oper_merchant_id = '"+omsOperList.getEntity_id()+"' ");
				}else{
					sql.append("and a.entity_id = '"+omsOperList.getEntity_id()+"' ");
				}
				list = operatorStationDao.findDataBySql(sql.toString());

				json.setList(list);
				json.setCode(JsonForList.SuccessCode);
			}
		}else{
			json.setCode(JsonForList.FailCode);
			json.setMsg("未找到该oper_id对应操作员");
		}
		
		return json;
	}
	
}
